// components/Team/selectTeam/selectTeam.js
import api from '../../../api/index'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: Boolean,
    player:Object
  },

  /**
   * 组件的初始数据
   */
  data: {
    start: "点击下拉选择",
    slist: [ "kyc.com", '21 ', '123' ,'21 ', '123' ,'21 ', '123' ], // 可选队伍数组
    isstart: false,
    selectTeam: {} // 选中了哪个队伍
  },

  /**
   * 组件的方法列表
   */
  attached(){
   
    var status = 'all'
    var type = 'create'
    var params = {status,type}
    api.team.list(params).then(res=>{
      console.log(res)
      this.setData({
        slist:res.result.data.list
      })
    })
  },
  methods: {
    opens: function (e) {
      switch (e.currentTarget.dataset.item) {
       case "1":
        if (this.data.isstart) {
         this.setData({
          isstart: false,
         });
        }
        else {
         this.setData({
          isstart: true,
         });
        }
        break;
      }
     },
     onclicks1: function (e) {
      var index = e.currentTarget.dataset.index;
      let name = this.data.slist[index];
      this.setData({
       isstart: false,
       isfinish: false,
       isdates: false,
       start: this.data.slist[index].name,
       selectTeam: this.data.slist[index],
       finish: "目的地"
      })
      console.log(this.data.selectTeam)
     },
     apply:function(){

      var teamID = this.data.selectTeam._id //需要组队列表获得呀
      var nickName = this.data.player.nickName
      var openid = this.data.player.openid
      var avatarUrl = this.data.player.avatarUrl
      var params = {teamID,nickName,openid,avatarUrl}
      api.player.apply(params).then(res=>{
        console.log(res)
        
        if(res.code==200){
          let pages = getCurrentPages();
        let prevPages = pages[pages.length - 2];
        prevPages.onLoad()
        wx.navigateBack({
          delta: 1,
        })
          wx.showToast({
            title: '邀请成功',
            icon: 'success',
            duration: 2000
          })
        }else if(res.code==300){
          wx.showToast({
            title: '该队员已在队伍中',
            icon: 'success',
            duration: 2000
          })
        }
        
        
      })
    },
  confirm() {
    this.apply()
    this.setData({
      show: false
    })
  },
  off_canvas: function(){
    this.data.show ? this.setData({show: false}) :this.setData({show: true});
   },
   cancel:function(){
     this.setData({
      show:false
     })
   }
  },
  getMyTeam:function(){
    var status = 'all'
    var type = 'create'
    console.log("srsr")
    var params = {status,type}
    api.team.list(params).then(res=>{
      console.log(res.result.data.list)
      this.setData({
        slist:res.result.data.list
      })
    })
  }
})
